var eway = jQuery.extend({

	/**
	 * Cyclic Div: Scroll a div content cyclicly
	 * @require: jQuery 1.5
	 * @require: jQuery Dimension
	 * @author: Nguyen Tuan Anh
	 * @version: 1.0.0 
	 */
	cyclicDiv: function(options) {
		var options = jQuery.extend({
			url: null,
			div: null,
			visible: 5,
			height: '140px',
			direction: 'up', // available options: up, down
			delay: 5 // seconds
		}, options);
		
		if ((options.div == null) || (options.url == null)) {
			return;
		}
		
		jQuery.ajax({
			type: 'GET',
			url: options.url,
			dataType: 'xml',
			success: function(response) {
				
				if (response == null) {
					return;
				}
				
				var root = response.getElementsByTagName('vip').item(0);
				
				if (root == null) {
					return;
				}
				
				var tableStart = '<table cellpadding="4" cellspacing="0" border="0" width="100%">';
				var tableEnd = '</table>';
				var nodeList = root.getElementsByTagName('commod');
				var items = new Array(nodeList.length);
				var node2 = root.childNodes.item(0);
				for (i = 0; i < root.childNodes.length; i ++) {
					var node = root.childNodes.item(i);
					items[i] = '<tr ' + ((i % 2 == 0) ? 'style="background-color: #f9f1e6"' : '') + '>';
					items[i] += '<td width="10%" valign="middle" style="font-size: 9pt; color: #A9A9A9"><img src="' + node.getAttribute('image') + '" width="30" height="20" border="0" /></td>';
					items[i] += '<td width="70%" valign="top" style="font-family: tahoma, verdana, arial; font-size: 9pt;"><a class="normal2" href="' + node.getAttribute('link') + '">' + node.getAttribute('text') + '</a></td>';
					items[i] += '<td width="20%" nowrap="nowrap" valign="middle" style="font-family: tahoma, verdana, arial; color: #655f5f; font-size: 8pt;">' + node.getAttribute('date') + '</td>';
					items[i] += '</tr>';
				}
				
				// Adjust the visible elements
				if (options.visible > root.childNodes.length) {
					options.visible = root.childNodes.length;
				}

				var startIndex = 0;
				function buildContent() {
					
					if (startIndex >= items.length) {
						startIndex = 0;
					}
					
					var tableBody = '';
					var counter = 0;
					for (i = startIndex; (i < items.length) && (counter <= options.visible); i ++, counter ++) {
						tableBody += items[i];
					}
					if (counter <= options.visible) {
						for (i = 0; i < (options.visible - counter); i ++) {
							tableBody += items[i];
						}
					}
					
					jQuery(options.div).html(tableStart + tableBody + tableEnd);
					
					startIndex ++;
				};
				
				setInterval(function() {
					buildContent();
				}, options.delay * 1000);
				
				buildContent();
				jQuery(options.div).css('display', 'block').css('height', options.height).css('overflow', 'hidden');
			}
		});
	},
	
	/**
	 * Fade news ticker
	 * @require: jQuery 1.5
	 * @author: Nguyen Tuan Anh
	 * @version: 1.0.0
	 */
	 newsTicker: function(options) {
	 	var options = jQuery.extend({
	 		url: null,
	 		div: null,
	 		cssClass: 'normal2',
	 		delay: 4 // second
	 	}, options);
	 	
	 	if ((options.div == null) || (options.url == null)) {
	 		return;
	 	}
	 	
		jQuery.ajax({
			type: 'GET',
			url: options.url,
			dataType: 'xml',
			success: function(response) {
				if (response == null) {
					return;
				}
				
				var root = response.getElementsByTagName('items').item(0);
				var items = new Array(root.childNodes.length);
				
				for (i = 0; i < root.childNodes.length; i ++) {
					var node = root.childNodes.item(i);
					items[i] = '<a class="' + options.cssClass + '" href="' + node.getAttribute('link') + '">';
					items[i] += node.getAttribute('text');
					items[i] += '</a>';
				}
				
				jQuery(options.div).css('height', '20px')
								   .css('overflow', 'hidden');
				
				var i = 0;
				function animate() {
				
					if (i >= items.length) {
						i = 0;
					}
					
					jQuery(options.div).hide()
									   .html(items[i ++])
									   .fadeIn(500);
					
				};
				
				// Recursively call the function
				window.setInterval(function() {
					animate();
				}, options.delay * 1000);
				
				animate();
			}
		});
	 },
	 
	 /**
	  * Box search
	  * @require: jQuery 1.5
	  * @author: Nguyen Tuan Anh
	  * @version: 1.0.0	  
	  */
	  ajaxsearch: function(options) {
	  	var options = jQuery.extend({
	  		handle: null,
	  		div: null,
	  		url: null,
	  		autoShow: false,
	  		imageHandle: null
	  	}, options);
	  	
	  	if ((options.div == null) || (options.url == null)) {
	  		return;
	  	}

  		var oldImage = jQuery(options.imageHandle).attr('src');
  		var newImage = oldImage.replace('_right', '_down');
	  	
	  	if (options.handle != null) {
	  		jQuery(options.handle).click(function() {
	  			jQuery(options.div).toggle('slow');
	  			
	  			// Swap the image handle
	  			if (jQuery(options.imageHandle).attr('src') == oldImage) {
	  				jQuery(options.imageHandle).attr('src', newImage);
	  			} else {
	  				jQuery(options.imageHandle).attr('src', oldImage);
	  			}
	  		});
	  	}
	  	
	  	jQuery.ajax({
	  		type: 'GET',
	  		url: options.url,
	  		dataType: 'html',
	  		success: function(response) {
	  			jQuery(options.div).html(response);
	  			
	  			if (options.autoShow) {
	  				jQuery(options.div).show('slow');
	  				jQuery(options.imageHandle).attr('src', newImage);
	  			}
			}
		});
	  },
	  
	 /**
	  * On-the-fly boxes
	  * @require: jQuery 1.5
	  * @author: Nguyen Tuan Anh
	  * @version: 1.0.0	  
	  */
	  flyBox: function(options) {
	  	var options = jQuery.extend({
	  		handle: null,
	  		sep: null,
	  		url: null,
	  		content: null,
	  	}, options); 
	  	
	  	if ((options.handle == null) 
	  		|| (options.sep == null)
	  		|| (options.url == null)
	  		|| (options.content == null)) {
	  		return;
	  	}
	  	
	  	jQuery(options.handle + ',' + options.content).mouseover(function() {
	  		jQuery(options.sep).hide();
	  		jQuery(options.handle).css('height', '48px').css('border-bottom', 'none');
	  		jQuery(options.content).show();
	  	}).mouseout(function() {
	  		jQuery(options.content).hide();
	  		jQuery(options.handle).css('height', '40px').css('border-bottom', '1px solid #fdcd74');
	  		jQuery(options.sep).show();
	  	});
	  	
	  }
	  
});

$(document).ready(function() {
	//loadFirebugConsole();
	// Fix PNG images error on IE6
	$.ifixpng('../images/pixel.gif');
	$('img[@src$=.png]').ifixpng();
});